cmake_minimum_required(VERSION 3.1.3)
project(gateway-server)

set(target "gateway_server")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")

set(proto_path ${CMAKE_CURRENT_SOURCE_DIR}/../proto)
set(proto_files base.proto file.proto friend.proto gateway.proto message.proto notify.proto speech.proto transmite.proto user.proto)

set(proto_h "")
set(proto_c "")
set(proto_srcs "")

foreach(proto_file ${proto_files})
    string(REPLACE ".proto" ".pb.cc" proto_c ${proto_file})
    string(REPLACE ".proto" ".pb.h" proto_h ${proto_file})
    if(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/${proto_c})
        add_custom_command(
            PRE_BUILD
            COMMAND protoc --cpp_out=${CMAKE_CURRENT_BINARY_DIR} -I ${proto_path} --experimental_allow_proto3_optional ${proto_path}/${proto_file}
            DEPENDS ${proto_path}/${proto_file}
            OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${proto_c}
            COMMENT "生成Protobuf框架代码文件:" ${CMAKE_CURRENT_BINARY_DIR}/${proto_c}
        )
    endif()
    list(APPEND proto_srcs ${CMAKE_CURRENT_BINARY_DIR}/${proto_c})
endforeach()

set(src_files "")
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/source src_files)
add_executable(${target} ${src_files} ${proto_srcs})
target_link_libraries(${target}
    gflags  # gflags依赖的动态库
    spdlog fmt # spdlog依赖的动态库
    brpc ssl crypto leveldb # brpc依赖的动态库
    protobuf  # protobuf依赖的动态库
    etcd-cpp-api cpprest curl # etcd依赖的动态库
    /usr/lib/x86_64-linux-gnu/libjsoncpp.so.19 # jsoncpp依赖的动态库
    hiredis redis++ # redis依赖的动态库
)

include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../common)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../odb)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../third/include)


INSTALL(TARGETS ${target} RUNTIME DESTINATION bin)
